package com.zy.pro.system.web;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.zy.core.web.BaseController;
import com.zy.pro.system.service.EmployeeService;
import com.zy.pro.system.web.form.GlobalForm;
import com.zy.pro.user.model.User;
import com.zy.pro.user.service.UserService;
import com.zy.pro.user.web.form.UserSearchForm;

/**
 * 验证控制器
 * add by 
 */
@Controller
@RequestMapping("/supply/globalController")
public class GlobalController extends BaseController {

	private static final Logger logger = Logger.getLogger(GlobalController.class);
	
	/**
	 * 系统管理员管理服务类
	 */
	@Resource(name = "employeeServiceImpl")
	private EmployeeService employeeService;
	
	/**
	 * 系统管理员管理服务类
	 */
	@Resource(name = "userServiceImpl")
	private UserService userService;
	
	/**
	 *	验证是否重复(存在,返回不通过/不存在,返回通过)
	 * @param form
	 */
	@RequestMapping("/findCheck")
	@ResponseBody
	public Map findCheck(GlobalForm form, HttpServletRequest request){
		JSONObject json = new JSONObject();
		json.put("status", "y");
		json.put("info", "Through the verification of information！");
		
		Long count;
		try {
			if((form.getTabName() != null && !"".equals(form.getTabName())) && form.getParam() != null && !"".equals(form.getParam())){
				if(!form.getParam().equals(form.getJson())){
					if("shopUser.shopUserName".equals(form.getName())){
						form.setName("shopUserName");
					}
					count = employeeService.findCheck(form);
					if(count > 0){
						json.put("status", "n");
						json.put("info", "Already exists, please input again。");
					}
					logger.info(form.getMsg() + "Through the verification of information！");
				}
			}
		} catch (Exception e) {
			logger.error(form.getMsg() + "Validation failed, already exists！",e);
		}
		return json;
	}
	
	
	/**
	 *	找回密码是验证用户名是否存在.(存在,则返回通过/不存在,返回不通过)
	 * @param form
	 */
	@RequestMapping("/findCheckUserName")
	@ResponseBody
	public Map findCheckUserName(GlobalForm form, HttpServletRequest request){
		JSONObject json = new JSONObject();
		json.put("status", "n");
		json.put("info", "Does not exist, please input again。");
		Long count;
		try {
			if((form.getTabName() != null && !"".equals(form.getTabName())) && form.getParam() != null && !"".equals(form.getParam())){
				if(!form.getParam().equals(form.getJson())){
					count = employeeService.findCheck(form);
					if(count > 0){
						json.put("status", "y");
						json.put("info", "Through the verification of information！");
					}
					logger.info(form.getMsg() + "Through the verification of information！");
				}
			}
		} catch (Exception e) {
			logger.error(form.getMsg() + "Validation failed, does not exist exists！",e);
		}
		return json;
	}
	
	/**
	 *	验证邮箱是否存在
	 * @param form
	 */
	@RequestMapping("/findCheckEmail")
	@ResponseBody
	public Map findCheckEmail(GlobalForm form, HttpServletRequest request){
		JSONObject json = new JSONObject();
		if("find".equals(form.getMsg())){
			json.put("status", "n");
			json.put("info", "Email was not found, please enter again。");
		}else{
			json.put("status", "y");
			json.put("info", "Through the verification of information！");
		}
		
		
		try {
			if(form.getParam() != null && !"".equals(form.getParam())){
				User user = new User();
				user.setEmail(form.getParam());
				if(!"".equals(form.getStatusName()) && form.getStatusName() != null){
					user.setUserName(form.getStatusName());
				}
				user = userService.findUserByEamilAndUserName(user);
				if(user != null){
					if("find".equals(form.getMsg())){
						json.put("status", "y");
						json.put("info", "Through the verification of information！");
					}else{
						json.put("status", "n");
						json.put("info", "Email has been in existence, please enter again。");
					}
					logger.info(form.getMsg() + "Through the verification of information！");
				}
			}
		} catch (Exception e) {
			logger.error(form.getMsg() + "Validation failed, already exists！",e);
		}
		return json;
	}
}
